package Amap;



import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author LIBOFEI
 * @ClassName Test
 * @description
 * @date 2021/12/1 16:32
 * @Version 1.0
 **/
public class Test {


    public static void main(String[] args) {
        double[] start = {116.41,39.9316};
        double[] end = {117.223,39.1096};
        //北京东城区-天津河西区，高德app显示130KM 自己测算114.97KM，访问高德接口137.1KM
        getDistance(start,end);
        //北京东城区-北京昌平区，高德app显示41KM 自己测算35.57KM，访问高德接口39.6KM
        gaode(start,end);
    }

    public static void getDistance(double[] start, double[] end) {

        double lon1 = (Math.PI / 180) * start[0];
        double lon2 = (Math.PI / 180) * end[0];
        double lat1 = (Math.PI / 180) * start[1];
        double lat2 = (Math.PI / 180) * end[1];

        // double Lat1r = (Math.PI/180)*(gp1.getLatitudeE6()/1E6);
        // double Lat2r = (Math.PI/180)*(gp2.getLatitudeE6()/1E6);
        // double Lon1r = (Math.PI/180)*(gp1.getLongitudeE6()/1E6);
        // double Lon2r = (Math.PI/180)*(gp2.getLongitudeE6()/1E6);

        // 地球半径
        double R = 6371;

        // 两点间距离 km，如果想要米的话，结果*1000就可以了
        double d = Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1)) * R;
        System.out.println("KM：" + new BigDecimal(d).setScale(2, RoundingMode.HALF_UP));
        System.out.println("M：" + new BigDecimal(d * 1000).setScale(2, RoundingMode.HALF_UP));
    }



    public static void gaode(double[] start, double[] end) {
        String key = "8525ce1b75a1e4a190fbef4ada4a0151";
        String url = "https://restapi.amap.com/v3/distance";
        String param = "origins=" + start[0] + "," + start[1] + "&destination=" + end[0] + "," + end[1] + "&key=" + key;
        String result = HttpUtil.doGet(url,param);
        System.out.println(result);
    }



}
